import 'package:aichat_flutter_wallet/module_common/provider/token_list_provider.dart';
import 'package:flutter/cupertino.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/utils/utils.dart';

import '../../kai_print.dart';


class WalletInfoNotifier with ChangeNotifier {
  WalletInfo? _walletInfo;
  final TokenListNotifier _tokenListNotifier;

  // final NoticeProvider _noticeProvider;
  // this._noticeProvider
  WalletInfoNotifier(
    this._tokenListNotifier,
  ) {
    // print('WalletInfoNotifier  ${this._tokenListNotifier}');
    // if (null != _walletInfo) {
    //   _tokenListNotifier.setTokenList(_walletInfo!);
    //   // _noticeProvider.refreshUnreadCount(wallet: wallet!);
    // }
  }

  setWalletInfo(WalletInfo? walletInfo) {
    _walletInfo = walletInfo;
    kaiPrint('setInitInfoNoUpdatePage ${_walletInfo}');
    _tokenListNotifier.setTokenList(_walletInfo!);
    notifyListeners();
  }

  setInitInfoNoUpdatePage(WalletInfo? walletInfo) {
    if (null != _walletInfo) {
      // print('setInitInfoNoUpdatePage ${_walletInfo}');
      _walletInfo = walletInfo;
      _tokenListNotifier.setTokenList(_walletInfo!);
    }
  }

  String get walletAddress => null == _walletInfo ? '' : _walletInfo!.address;

  String get walletFormatAddress =>
      null == _walletInfo ? '' : Utils.formatAddress(_walletInfo!.address);

  String get walletName => null == wallet ? '' : wallet!.getName();

  Chain? get walletChain => _walletInfo?.getChain();

  String get currentRpc =>
      ObjectBox()
          .nodeInfoModel
          .getSelectedNodeInfo(walletChain!.shortName)
          .url ??
      '';

  String get walletChainShortName =>
      null == _walletInfo ? '' : _walletInfo!.chainShortName;

  int get walletId => null == _walletInfo ? 0 : _walletInfo!.walletId;

  Wallet? get wallet => ObjectBox().walletModel.getWalletById(walletId);

  WalletInfo? get walletInfo => _walletInfo;

  bool get hasOmpWallet => null != getWalletInfoByChain(ChainList.ompChain);

  bool get hasOmpWalletNoWatch =>
      !isWatchWallet() && null != getWalletInfoByChain(ChainList.ompChain);

  bool isWatchWallet() {
    Wallet? w = wallet;
    return null != w && w.isWatchWallet();
  }

  WalletInfo? getWalletInfoByChain(Chain c) {
    List<WalletInfo> walletInfoList =
        ObjectBox().walletInfoModel.getWalletInfoListByWalletId(walletId);
    for (var w in walletInfoList) {
      if (w.chainShortName == c.shortName) {
        return w;
      }
    }

    return null;
  }

  void updateCurrentWallet(WalletInfo info) {
    Config.logger.d('update current wallet $info');

    _walletInfo = info;
    notifyListeners();
    // final userId = NativeChannelBridge.instance.appCommMsg?.userId ?? '';
    // SP().setCurrentWalletInfoId('${userId},${info.id}');
    // 记录当前选中 的 是哪个 钱包
    SP().setWalletSelectedInfoId(walletId, info.id);

    _tokenListNotifier.updateTokenList(info);
    // _noticeProvider.refreshUnreadCount();
  }

  void updateBtcWalletInfoAddress(String address) {
    _walletInfo?.address = address;
    ObjectBox().updateBtcWalletInfoAddress(_walletInfo!.id, address);
    notifyListeners();
    _tokenListNotifier.updateTokenList(_walletInfo!);
    // _noticeProvider.refreshUnreadCount();
  }

  updateWalletName(Wallet wallet, String name) {
    ObjectBox().walletModel.updateWalletName(wallet.id, name);
    if (wallet.id == walletId) {
      notifyListeners();
    }
  }

  updateWalletBackupState(int wallet_id) {
    ObjectBox().walletModel.updateWalletBackupState(wallet_id);
    if (wallet_id == walletId) {
      notifyListeners();
    }
  }

  updateWalletGoogleFileId(String value) {
    ObjectBox().walletModel.updateWalletGoogleFileId(wallet, value);
  }

  updateWalletAppleFileId(String value) {
    ObjectBox().walletModel.updateWalletAppleFileId(wallet, value);
  }

  updateWalletServer() {
    ObjectBox().walletModel.updateWalletKeyEmpty(wallet, 3);
  }

  List<Chain> getWalletChainList() {
    List<Chain> chains = [];
    List<WalletInfo> infos =
        ObjectBox().walletInfoModel.getWalletInfoListByWalletId(walletId);
    for (var w in infos) {
      Chain? c = w.getChain();
      if (null != c) {
        chains.add(c);
      }
    }
    return chains;
  }
}
